package com.miao.admin.controller;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.miao.admin.service.AdminRecommendService;
import com.miao.admin.service.AdminReductionService;
import com.miao.common.entity.R;
import com.miao.common.entity.pojo.Reduction;
import com.miao.common.entity.pojo.ReductionApply;
import com.miao.common.entity.pojo.ReductionApply;
import com.miao.common.entity.pojo.ReductionApply;
import com.miao.admin.service.ReductionApplyService;
import com.miao.common.utils.JWTUtil;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 首页降价优惠表申请(ReductionApply)表控制层
 *
 * @author MiaoWenQing
 * @since 2021-02-19 00:17:21
 */
@RestController
@RequestMapping("admin/reductionApply")
public class ReductionApplyController  {
    /**
     * 服务对象
     */
    @Resource
    private ReductionApplyService reductionApplyService;

    @Resource
    private AdminReductionService adminReductionService;




    /**
     * 申请
     * @param reductionApply 表单
     * @return R
     */
    @PostMapping("/apply")
    public R addApply(@RequestBody  ReductionApply reductionApply){

        reductionApply.setApplyTime(new Date());
        if (this.reductionApplyService.save(reductionApply)) {
            return R.ok();
        }
        return R.error();
    }


    /**
     * 通过申请
     * @return R
     */
    @GetMapping("/byApply")
    public R byApply(String id, @RequestHeader(value = "access-token") String token){


        ReductionApply reductionApply = this.reductionApplyService.getById(id);
        reductionApply.setOpreaTime(new Date());
        reductionApply.setAdminId(Integer.parseInt(this.getAdminId( token)));
        reductionApply.setState(2);


        Reduction reduction = this.adminReductionService.getById(reductionApply.getPostion());
        if (null!=reduction.getRedId()){
            this.optionApply(reduction.getRedId(),this.getAdminId( token),3);
        }
        reduction.setProId(reductionApply.getProId());
        reduction.setRedId(reductionApply.getRedId());
        this.adminReductionService.updateById(reduction);

        if (this.reductionApplyService.updateById(reductionApply)) {
            return R.ok();
        }
        return R.error();


    }

    /**
     * 获取店铺 的申请
     * @param storeId 店铺id
     * @return R
     */
    @GetMapping("/applyListStore")
    public R applyListStore(String storeId){

        QueryWrapper<ReductionApply> reductionApplyQueryWrapper = new QueryWrapper<>();
        reductionApplyQueryWrapper.eq("store_id",storeId);
        reductionApplyQueryWrapper.orderByDesc("apply_time");
        List<ReductionApply> list = this.reductionApplyService.list(reductionApplyQueryWrapper);
        return R.ok(list);
    }

    /**
     * 获取全部申请
     * @return R
     */
    @GetMapping("/applyList")
    public R applyList(){

        QueryWrapper<ReductionApply> reductionApplyQueryWrapper = new QueryWrapper<>();
        reductionApplyQueryWrapper.orderByDesc("apply_time");
        List<ReductionApply> list = this.reductionApplyService.list(reductionApplyQueryWrapper);
        return R.ok(list);


    }
    /**
     * 拒绝申请
     * @return R
     */
    @GetMapping("/refApply")
    public R refApply(String id, @RequestHeader(value = "access-token") String token){


        if (this.optionApply(id,this.getAdminId( token),1)) {
            return R.ok();
        }
        return R.error();

    }



    /**
     * 下架申请
     * @return R
     */
    @GetMapping("/offApply")
    public R offApply(String id, @RequestHeader(value = "access-token") String token){
        if (this.optionApply(id,this.getAdminId( token),3)) {
            return R.ok();
        }
        return R.error();

    }




    public boolean optionApply(String id , String adminId, int state){
        ReductionApply reductionApply = this.reductionApplyService.getById(id);
        reductionApply.setOpreaTime(new Date());
        reductionApply.setAdminId(Integer.parseInt(adminId));
        reductionApply.setState(state);
        return this.reductionApplyService.updateById(reductionApply);

    }

    private String getAdminId(String token) {
        String s = JWTUtil.analysisToken(token);
        Map map = JSON.parseObject(s, Map.class);
        return map.get("adminId").toString();//1平台 0店铺


    }




}
